package com.cscec5b.inspection.service;

import com.cscec5b.inspection.entity.RebarRecord;
import com.cscec5b.inspection.repository.RebarRecordRepository;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RebarRecordAppService {

    private final RebarRecordRepository repo;

    public RebarRecordAppService(RebarRecordRepository repo) {
        this.repo = repo;
    }

    public Map<String,Object> save(RebarRecord body){
        RebarRecord saved = repo.save(body);
        Map<String,Object> ret = new HashMap<>();
        ret.put("ok", true);
        ret.put("id", saved.getId());
        ret.put("data", saved);
        return ret;
    }

    public Map<String,Object> detail(long id){
        return Map.of("data", repo.findById(id).orElse(null));
    }

    public List<RebarRecord> listByProject(long projectId){
        return repo.findByProjectIdOrderByIdDesc(projectId);
    }
}
